<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-20 14:16:12
 * @LastEditors: wangming
 * @LastEditTime: 2022-02-10 10:28:04
 * @Description: file content
-->
<template>
  <div>
    <el-row :gutter="12">
      <!-- <el-col :span="6">
        <el-input
          v-model="queryData.fileName"
          placeholder="报表名称"
          clearable
          :maxlength="64"
          prefix-icon="el-icon-search"
        />
      </el-col> -->
      <!-- <el-col :span="6">
        <el-button type="primary" @click="loadData">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-col> -->
      <el-col class="mt10">
        <el-button :disabled="!canAddLang" type="primary" @click="handleCreate">新建</el-button>
      </el-col>
    </el-row>
    <el-table
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
    >
      <el-table-column prop="region" label="国家地区">
        <template slot-scope="scope">
          <span>{{ scope.row.region }}</span>
          <IconPark
            v-if="scope.row.status == 'DEFAULT'"
            style="vertical-align: middle; margin-left: 10px"
            size="16"
            theme="outline"
            type="key"
            fill="#409EFF"
          />
        </template>
      </el-table-column>
      <el-table-column prop="key" label="语言标识" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <!-- {{ scope.row }} -->
          <span
            :style="{
              color: scope.row.status !== 'DISABLE' ? '#1aad19' : '#f00505',
            }"
            >{{ scope.row.status !== "DISABLE" ? "启用" : "禁用" }}</span
          >
        </template>
      </el-table-column>

      <el-table-column prop="actions" label="操作">
        <template slot-scope="scope">
          <template>
            <!-- <span class="table-actionItem"
              ><el-button type="text" @click="handleEdit(scope.row)"
                >编辑</el-button
              ></span
            > -->
            <!-- {{scope.row}} -->
            <span class="table-actionItem" v-if="scope.row.status == 'ENABLE' && scope.row.key !== 'zh-CN'">
              <el-popconfirm
                title="确认要禁用该语言标识吗？"
                placement="top"
                @confirm="handleUpdateStatus(scope.row, 'DISABLE')"
              >
                <el-button type="text" slot="reference">禁用</el-button>
              </el-popconfirm>
            </span>
            <span class="table-actionItem" v-if="scope.row.status == 'DISABLE'">
              <el-popconfirm
                title="确认要启用该语言标识吗？"
                placement="top"
                @confirm="handleUpdateStatus(scope.row, 'ENABLE')"
              >
                <el-button type="text" slot="reference">启用</el-button>
              </el-popconfirm>
            </span>
            <span class="table-actionItem" v-if="scope.row.status == 'ENABLE'">
              <el-popconfirm
                title="确认要设置为默认语言标识吗？"
                placement="top"
                @confirm="handleUpdateStatus(scope.row, 'DEFAULT')"
              >
                <el-button type="text" slot="reference">设为默认</el-button>
              </el-popconfirm>
            </span>
            <span
              class="table-actionItem"
              v-if="
                !(scope.row.status === 'DEFAULT' || scope.row.key === 'zh-CN')
              "
            >
              <el-popconfirm
                title="确定要删除该语言标识吗？"
                placement="top"
                @confirm="handleDelete(scope.row)"
              >
                <el-button class="link-danger" type="text" slot="reference"
                  >删除</el-button
                >
              </el-popconfirm>
            </span>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="tr mt24">
      <x-pagination
        :total="total"
        :pageSize.sync="pageData.pageSize"
        :pageNo.sync="pageData.pageNo"
        @search="loadData"
      />
    </div>
  </div>
</template>

<script>
import { IconPark } from "@icon-park/vue/es/all";
import LangCodeModal from "./langCodeModal/index";
import { LANG_CODE } from "@/utils/enum";
import {
  getHmi18nlocalePageList,
  deleteHmi18nlocale,
  putHmi18nlocaleStatusByKeyByStatus,
  postHmi18nlocale,
} from "@/apis/paas/HmI18nLocaleController.js";
export default {
  name: "LangCode",
  components: { IconPark, LangCodeModal },
  computed: {
    canAddLang() {
      return this.tableData.length < LANG_CODE.length;
    }
  },
  data() {
    return {
      loading: false,
      queryData: {},
      pageData: {
        pageSize: 10,
        pageNo: 1,
      },
      tableData: [],
      total: 0,
    };
  },

  created() {
    this.loadData();
  },

  methods: {
    loadData() {
      getHmi18nlocalePageList(this.pageData).then((res) => {
        this.tableData = res.data;
        this.total = res.totalCount;
      });
    },

    // handleReset() {
    //   this.loadData();
    // },

    handleCreate() {
      LangCodeModal.addForm(this.tableData).then((data) => {
        postHmi18nlocale(data).then((res) => {
          // console.log(res);
          // if (res.code == 0) {
          this.loadData();
          // this.$message.success("操作成功");
          // } else {
          // this.$message.error("操作失败");
          // return;
          // }
        });
      });
    },

    // handleEdit(record) {
    //   LangCodeModal.editForm({
    //     ...record,
    //   });
    // },

    handleUpdateStatus(record, status) {
      putHmi18nlocaleStatusByKeyByStatus({
        key: record.key,
        status: status,
      }).then((res) => {
        // if (res.code === 0) {
        this.loadData();
        // this.$message.success("操作成功");
        // } else {
        // this.$message.error("操作失败");
        // }
      });
    },

    handleDelete(record) {
      deleteHmi18nlocale([record.key]).then((res) => {
        // if (res.code === 0) {
        this.loadData();
        // this.$message.success("操作成功");
        // } else {
        // this.$message.error("操作失败");
        // }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
